// bupiao.js  红包
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
var app = getApp()
var winScale = 750 / app.data.deviceInfo.windowWidth
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 20,
    currentTab: 0,
    all_bupiao_list: [],
    bupiaolistflag: true,
    showLoading: true,
    bupiaoflag: false,
    res_navbar: ['获取明细', '购物支出', '提现记录'],
    reminder: [{
      'tit': '您还没有获取记录',
      'word': '未来的你会感谢现在正在奋斗的自己~'
    }, {
      'tit': '您还没有购物支出',
      'word': '幸福的秘诀在于，爱自己、爱生活~'
    }, {
      'tit': '您还没有提现记录',
      'word': '即使现在囊空如洗，我也能看到未来的富有~'
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var res = wx.getSystemInfoSync()
    this.setData({
      winHeight: res.windowHeight * winScale - 558
    })
    this.changebupiaolist(this.data.currentTab)
  },
  onResNavbarTap: function(e) {
    // console.log(e)
    this.setData({
      page: 1,
      all_bupiao_list: [],
      showLoading: true,
      currentTab: e.currentTarget.dataset.index
    });
    this.changebupiaolist(this.data.currentTab)
  },
  changebupiaolist: function(tabindex) {
    switch (Number(tabindex)) { //get_bupiao_trade
      case 0:
        this.refreshbupiaolist(config.get_balance_gain)
        break;
      case 1:
        this.refreshbupiaolist(config.get_balance_order)
        break;
      case 2:
        this.refreshbupiaolist(config.get_balance_cash)
        break;
    }
  },
  refreshbupiaolist: function(a) {
    var that = this
    shihan.sendRequest(a, 'POST', {
        page: this.data.page,
        page_size: this.data.page_size
      })
      .then(function(response) {
        if (response.data.code == 0) {
          that.setData({
            userbalance: response.data.data.sh_balance_info.sh_user_balance
          })
          if (Number(response.data.data.sh_balance_info.sh_user_balance) == 0) {
            response.data.data.sh_balance_info.sh_user_balance = Number(response.data.data.sh_balance_info.sh_user_balance).toFixed(2)
          }
          if (Number(response.data.data.sh_balance_info.sh_freeze_amount) == 0) {
            response.data.data.sh_balance_info.sh_freeze_amount = Number(response.data.data.sh_balance_info.sh_freeze_amount).toFixed(2)
          }
          that.setData({
            bupiaoover: response.data.data.sh_balance_info,
            page: that.data.page + 1,
            all_bupiao_list: that.data.all_bupiao_list.concat(response.data.data.sh_items),
            showLoading: false,
            bupiaolistflag: true
          })
          console.log(that.data.userbalance)
          console.log(that.data.all_bupiao_list.length)
        } else {
          that.setData({
            showLoading: false,
            bupiaolistflag: false
          })
        }
      }, function(error) {
        console.log(error);
      });
  },
  opendetail: function(e) {
    // if (this.data.currentTab == 2) {
    wx.navigateTo({
      url: '/pages/other/balancedetail/balancedetail?id=' + e.currentTarget.dataset.id + '&index=' + this.data.currentTab
    })
    // }
  },
  bupiaodetail: function() { //查看红包详情
    wx.navigateTo({
      url: '/pages/strategyinfo/strategyinfo?share=0&nologin=1&url=' + encodeURIComponent(config.commonurl + 'h5/index/balancenew.html')
    })
  },
  closebupiao: function() {
    this.setData({
      bupiaoflag: false
    })
  },
  loadMoreData: function() {
    var that = this
    if (that.data.bupiaolistflag) {
      that.setData({
        bupiaolistflag: false
      })
      this.changebupiaolist(this.data.currentTab)
    }
  },
  withdrawcrash: function() {
    wx.showModal({
      title: "提示",
      content: '请在世瀚百年APP内申请提现服务（使用微信登录）',
      confirmText: "确认",
      confirmColor: "#c40000",
      showCancel: true,
      success: function(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/strategyinfo/strategyinfo?share=0&nologin=1&url=' + encodeURIComponent(config.commonmoburl + 'other/downloadapp.html')
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    // var that = this
    // if (that.data.bupiaolistflag) {
    //   that.setData({
    //     bupiaolistflag: false
    //   })
    //   this.changebupiaolist(this.data.currentTab)
    // }
  }
})